//package com.lagou.edu.component;
//
//import com.google.common.collect.Lists;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.dubbo.common.threadpool.support.fixed.FixedThreadPool;
//
//import java.util.Comparator;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.Executors;
//import java.util.concurrent.TimeUnit;
//import java.util.stream.Collectors;
//
///**
// * @program: stage-3-module-3
// * @description:
// * @author: zhangxiaoyang
// * @date: 2020-09-02 15:19
// **/
//public class TPMonitorThread implements Runnable {
//
//    private static final double ALARM_90_PERCENT = 0.90;
//    private static final double ALARM_99_PERCENT = 0.99;
//    public static final Map<String, Integer> METHOD_CALL = new ConcurrentHashMap<>();
//    public static final Map<String, Map<Long, Long>> METHOD_RESPONSE_TIME = new ConcurrentHashMap<>();
//
//    public TPMonitorThread() {
//        // 每隔5秒打印接口性能数据
//        Executors.newSingleThreadScheduledExecutor()
//                 .scheduleWithFixedDelay(this, 1, 5, TimeUnit.SECONDS);
//    }
//
////    @Override
////    public Executor getExecutor(
////            URL url) { // 从父类中创建线程池
////        final Executor executor = super.getExecutor(url);
////        if (executor instanceof ThreadPoolExecutor) {
////            THREAD_POOLS.put(url, ((ThreadPoolExecutor) executor));
////        }
////        return executor;
////    }
//
//
//    @Override
//    public void run() {
//        for (Map.Entry<String, Integer> callEntry : METHOD_CALL.entrySet()) {
//            String methodName1 = callEntry.getKey();
//            for (Map.Entry<String, Map<Long, Long>> rtEntry : METHOD_RESPONSE_TIME.entrySet()) {
//                String methodName2 = rtEntry.getKey();
//                if (StringUtils.equals(methodName1, methodName2)) {
//                    List<Long> latestCallList = rtEntry.getValue()
//                                                       .keySet()
//                                                       .stream()
//                                                       .filter(key -> new Date().getTime() - key <= 1 * 60 * 1000)
//                                                       .collect(Collectors.toList());
//                    List<Long> responseTimeList = Lists.newLinkedList();
//                    for (Map.Entry<Long, Long> entry : rtEntry.getValue()
//                                                              .entrySet()) {
//                        if (latestCallList.contains(entry.getKey())) {
//                            responseTimeList.add(entry.getValue());
//                        }
//                    }
//                    List<Long> sortedList = responseTimeList.stream()
//                                                            .sorted(Comparator.comparingLong(value -> value))
//                                                            .collect(Collectors.toList());
//                    int tp90Indicator = (int) (callEntry.getValue() * ALARM_90_PERCENT);
//                    System.out.println(callEntry.getKey() + "方法的TP90为：" + sortedList.get(tp90Indicator));
//                    int tp99Indicator = (int) (callEntry.getValue() * ALARM_99_PERCENT);
//                    System.out.println(callEntry.getKey() + "方法的TP99为：" + sortedList.get(tp99Indicator));
//                    break;
//                }
//            }
//        }
//    }
//}
